#!/bin/bash

function add_development_us_database(){
	local db_arr  db_less_name db_big_name db_name
	db_arr=`echo $1|sed "s/\[/ /g;s/,/ /g;s/\]/ /g"`
	cat << 'EOF' > ../conf/db_config/development_us_database.php
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$active_group = 'default';
$active_record = TRUE;

$common_db_config = array (
                "dbdriver" => "mysql",
                "dbprefix" => "",
                "pconnect" => FALSE,
                "db_debug" => TRUE,
                "cache_on" => FALSE,
                "cachedir" => "",
                "char_set" => "utf8",
                "dbcollat" => "utf8_general_ci",
                "swap_pre" => "",
                "autoinit" => TRUE,
                "stricton" => FALSE
);

$db ['default'] ['hostname'] = '127.0.0.1';
$db ['default'] ['username'] = 'k9pf';
$db ['default'] ['password'] = 'k9_01';
$db ['default'] ['database'] = 'k9pf';
$db ['default'] ['dbdriver'] = 'mysql';
$db ['default'] ['dbprefix'] = '';
$db ['default'] ['pconnect'] = FALSE;
$db ['default'] ['db_debug'] = TRUE;
$db ['default'] ['cache_on'] = FALSE;
$db ['default'] ['cachedir'] = '';
$db ['default'] ['char_set'] = 'utf8';
$db ['default'] ['dbcollat'] = 'utf8_general_ci';
$db ['default'] ['swap_pre'] = '';
$db ['default'] ['autoinit'] = TRUE;
$db ['default'] ['stricton'] = FALSE;

$db ['k9pack'] = array_merge ( array (
                "hostname" => "127.0.0.1",
                "username" => "root",
                "password" => "",
                "database" => "k9pack"
), $common_db_config );

$db ['k9ope'] = array_merge ( array (
                "hostname" => "127.0.0.1",
                "username" => "k9ope",
                "password" => "k9_01",
                "database" => "k9ope"
), $common_db_config );
        $slave_count = 1;
        $k9_host_m = '127.0.0.1'; // 'user_m.k9.local';
        $k9_host_s = '127.0.0.1'; // 'user_s.k9.local';
EOF

#################
####### 1 #######
#################
        for db_name in ${db_arr[@]}
        do
                db_var_name=`echo $db_name|sed "s/k9/k9_/g"`
                db_name_pass=$db_name
                if [ "$db_name" == "k9transaction" ]
                then
                        db_name_pass=k9pay
                fi
                echo "
// $db_name db
\$${db_var_name}_db =   \"$db_name\";
\$${db_var_name}_db_user_m      =       \$${db_var_name}_db;//.\"_m\";
\$${db_var_name}_db_pwd_m       =       \"k9_01\";
\$${db_var_name}_db_user_s      =       \$${db_var_name}_db;//.\"_s\";
\$${db_var_name}_db_pwd_s       =       \"k9_01\"; " >> ../conf/db_config/development_us_database.php
        done


#################
####### 2 ####### 
#################
	for db_name in ${db_arr[@]}
	do
		db_less_name=`echo $db_name|sed "s/k9//g"`
		db_big_name=`tr [a-z] [A-Z] <<< $db_name|sed "s/K9/K9_/g"`
		db_var_name=`echo $db_name|sed "s/k9/k9_/g"`
		echo "
// read db of $db_name notification info
\$DB_${db_big_name}_R = \$${db_var_name}_db . \"_r\";
\$slave_db_${db_less_name}_config = array_merge ( array (
		\"username\" => \"\$${db_var_name}_db_user_s\",
		\"password\" => \"\$${db_var_name}_db_pwd_s\",
		\"database\" => \"\$${db_var_name}_db\" 
), \$common_db_config );  " >> ../conf/db_config/development_us_database.php
	done

#################
####### 3 ####### 
#################
cat << 'EOF' >> ../conf/db_config/development_us_database.php
for($i = 0; $i < $slave_count; $i ++) {
	$suffix = $i == 0 ? '' : $i;
	eval ( "\$slave_hostname = \$k9_host_s$suffix;" );
EOF

#################
####### 4 ####### 
#################

	for db_name in ${db_arr[@]}
	do
		db_less_name=`echo $db_name|sed "s/k9//g"`
		db_big_name=`tr [a-z] [A-Z] <<< $db_name|sed "s/K9/K9_/g"`
		echo "	\$ci_db_${db_less_name}_name = \$DB_${db_big_name}_R . \$suffix;" >>  ../conf/db_config/development_us_database.php
	done


#################
####### 5 ####### 
#################
	echo '
	$hostname = array (
			"hostname" => $slave_hostname 
	);' >>  ../conf/db_config/development_us_database.php


#################
####### 6 ####### 
#################
	for db_name in ${db_arr[@]}
        do
                db_less_name=`echo $db_name|sed "s/k9//g"`
		db_big_name=`tr [a-z] [A-Z] <<< $db_name|sed "s/K9/K9_/g"`
                echo "	\$db [\$ci_db_${db_less_name}_name] = array_merge ( \$hostname, \$slave_db_${db_less_name}_config );" >>  ../conf/db_config/development_us_database.php
        done


#################
####### 7 ####### 
#################
	for db_name in ${db_arr[@]}
        do
                db_less_name=`echo $db_name|sed "s/k9//g"`
		db_big_name=`tr [a-z] [A-Z] <<< $db_name|sed "s/K9/K9_/g"`
                echo "	\$db_${db_name}_array [] = \$ci_db_${db_less_name}_name;" >>  ../conf/db_config/development_us_database.php
	done

#################
####### 8 ####### 
#################
echo '}
$ci = &get_instance();' >> ../conf/db_config/development_us_database.php


#################
####### 9 ####### 
#################
	for db_name in ${db_arr[@]}
        do
		echo "\$ci->config->set_item ( 'db_${db_name}_array',\$db_${db_name}_array);" >> ../conf/db_config/development_us_database.php
	done


#################
####### 10 ####### 
#################
        for db_name in ${db_arr[@]}
        do
		db_big_name=`tr [a-z] [A-Z] <<< $db_name|sed "s/K9/K9_/g"`
		db_var_name=`echo $db_name|sed "s/k9/k9_/g"`
		echo " 
// write db of $db_name user info
\$DB_${db_big_name}_W = \$${db_var_name}_db.'_w';
\$db[\$DB_${db_big_name}_W] = array_merge(array(
	\"hostname\"	=> \"\$k9_host_m\",
	\"username\"	=>	\"\$${db_var_name}_db_user_m\",
	\"password\" 	=> 	\"\$${db_var_name}_db_pwd_m\",
	\"database\" => \"\$${db_var_name}_db\"),
\$common_db_config);
" >> ../conf/db_config/development_us_database.php
	done
}

add_development_us_database $1
